package br.com.siscov.control.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import br.com.siscov.control.form.DadosAltClienteForm;
import br.com.siscov.model.cliente.service.ClienteService;
import br.com.siscov.model.vo.ClienteVo;
import br.com.siscov.util.Constantes;

public class ConsultarClienteAction extends Action {

	@SuppressWarnings("deprecation")
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		ActionErrors errors = new ActionErrors();

		try {

			DadosAltClienteForm dadosAltClienteForm = (DadosAltClienteForm) form;

			ClienteVo clienteVo = ClienteService.obterCliente(dadosAltClienteForm.getCpfCnpj());
			if (dadosAltClienteForm.getCpfCnpj().equals(clienteVo.getCpfCnpj())) {

				request.setAttribute("clienteVo", clienteVo);
				return mapping.findForward(Constantes.FORWARD_SUCESSO);
			}

			errors.add("errors", new ActionMessage("errors.sql.dados.inexistente"));
			saveErrors(request, errors);
			return mapping.findForward(Constantes.FWD_ERRO);

		} catch (Exception e) {
			errors.add("errors", new ActionMessage("errors.sql.existente"));
			saveErrors(request, errors);
			return mapping.findForward(Constantes.FWD_ERRO);
		}

	}

}
